package com.bolang.onlycode.demo.configuratin;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Configuration
public class ThreadPrintConfig {
    @Bean(name = "threadPoolPrintExecutor")
    public ThreadPoolTaskExecutor threadPoolPrintExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数目
        executor.setCorePoolSize(1);
        //指定最大线程数
        executor.setMaxPoolSize(1);
        //队列中最大的数目
        executor.setQueueCapacity(2 * 10);
        //线程名称前缀
        executor.setThreadNamePrefix("ThreadPoolPrintExecutor-");
        //rejection-policy：当pool已经达到max size的时候，如何处理新任务
        //CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
        //对拒绝task的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //当调度器shutdown被调用时等待当前被调度的任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //线程空闲后的最大存活时间
        executor.setKeepAliveSeconds(0);
        //加载
        executor.initialize();
        return executor;
    }
//    @Bean(name = "threadPoolQueryExecutor")
//    public ThreadPoolTaskExecutor threadPoolQueryExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        //核心线程数目
//        executor.setCorePoolSize(6);
//        //指定最大线程数
//        executor.setMaxPoolSize(12);
//        //队列中最大的数目
//        executor.setQueueCapacity(2 * 10);
//        //线程名称前缀
//        executor.setThreadNamePrefix("ThreadPoolQueryExecutor-");
//        //rejection-policy：当pool已经达到max size的时候，如何处理新任务
//        //CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
//        //对拒绝task的处理策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        //当调度器shutdown被调用时等待当前被调度的任务完成
//        executor.setWaitForTasksToCompleteOnShutdown(true);
//        //线程空闲后的最大存活时间
//        executor.setKeepAliveSeconds(30);
//        //加载
//        executor.initialize();
//        return executor;
//    }
}
